# -*- coding: utf-8 -*-
from wt.connector.db_manager.db_untils_tools import DBUntilsTools
from wt.connector.data_connect import DataConnect, MongoDBOperate


class DBMongoManager(DataConnect, DBUntilsTools):
    def __init__(self):
        super().__init__()

    def generate_create_ddl(self, db, table):
        """
        拼接mongodb ddl语句
        :param table:
        :return:
        """
        ddl_sql = self.general_col_value()
        return MongoDBOperate(operate="create_ddl", data=ddl_sql, table=table)

    def general_col_value(self, ):
        """
        生成 mongodb dml
        :param table:
        :return:
        """
        id = self.get_random_id()
        c_02_integer_val = self.get_random(-2147483648, 2147483648)
        c_03_double_val = self.get_random_float(-500, 500)
        c_04_float_val = self.get_random_float(-500, 500)
        c_05_smallint_val = self.get_random(-32768, 32767)
        c_06_bigint_val = self.get_random(-9223372036854775808, 9223372036854775807)
        c_07_tinyint_val = self.get_random(-128, 123)
        c_08_decimal_val = self.get_random_float(-500, 500)
        c_09_varchar_val = self.get_random_string(8)
        c_10_date_val = self.get_random_date()
        c_11_datetime_val = self.get_random_datetime()

        document = {
            "id": id,
            "c_02_integer_col": c_02_integer_val,
            "c_03_double_col": c_03_double_val,
            "c_04_float_col": c_04_float_val,
            "c_05_smallint_col": c_05_smallint_val,
            "c_06_bigint_col": c_06_bigint_val,
            "c_07_tinyint_col": c_07_tinyint_val,
            "c_08_decimal_col": c_08_decimal_val,
            "c_09_varchar_col": c_09_varchar_val,
            "c_10_date_col": c_10_date_val,
            "c_11_datetime_col": c_11_datetime_val
        }
        return document

    def generate_dml_sql(self, table, lines):
        """
        批量生成 mongodb dml 语句
        :param table:
        :param lines:
        :return:
        """
        documents = []
        while lines > 0:
            documents.append(self.general_col_value(table))
            lines -= 1
        return documents

    def execute_create_and_insert_sql(self, db, table, num, is_recreate=None):
        """
        生成ddl、插入dml
        :param is_recreate:
        :param db:
        :param table:
        :param num:
        :return:
        """
        if is_recreate:
            self.exec_mongodb_sql(db, MongoDBOperate(operate="drop", data=None, table=table))

        documents = self.generate_dml_sql(table, num)
        self.exec_mongodb_sql(db, MongoDBOperate(operate="insert_many", data=documents, table=table))

    def show_table_sql(self, db, table):
        result = self.exec_mongodb_sql(db, MongoDBOperate(operate="find_one", data=None, table=table))
        print(result)

    def create_and_insert_sql(self, db, table, num, is_recreate=None):
        """
        生成ddl、插入dml
        :param is_recreate:
        :param db:
        :param table:
        :param num:
        :return:
        """
        if is_recreate:
            self.exec_mongodb_sql(db, MongoDBOperate(operate="drop", data=None, table=table))

        documents = self.generate_dml_sql(table, num)
        self.exec_mongodb_sql(db, MongoDBOperate(operate="insert_many", data=documents, table=table))

    def get_count(self, db, table):
        count = self.exec_mongodb_sql(db, MongoDBOperate(operate="count_documents", data={}, table=table))
        return count

    def execute_drop_table(self, db, table):
        res = self.exec_mongodb_sql(db, MongoDBOperate(operate="drop", data=None, table=table))
        if res['data'] == "True":
            return res['data']
        else:
            return None


if __name__ == '__main__':
    # db_name = "test"
    # collection_name = "test_mongo"
    # a = DBMongoManager()
    # a.create_and_insert_sql(db_name, collection_name, 10, True)
    # count = a.get_count(db_name, collection_name)
    # print(count)
    a = DBMongoManager()
    sql = a.generate_create_ddl("wls", "mo")
    t1 = a.exec_mongodb_sql(sql)
    print(t1)
